package com.zzyl.nursing.job;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.mapper.ReservationMapper;
import com.zzyl.nursing.service.ReservationService;
import com.zzyl.nursing.service.impl.ReservationServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Component
@Slf4j
public class ReservationExpiredTask {

    @Autowired
   private ReservationMapper reservationMapper;

    @Scheduled(cron = "0 1/31 * * * ?")
    public void UpdateRervationSatus(){
        log.info("开始检查预约状态和修改预约状态......");
        LocalDateTime now = LocalDateTime.now();
        QueryWrapper<Reservation> queryWrapper = new QueryWrapper<>();
        queryWrapper.lt("time",now);
        queryWrapper.eq("status",0);
        queryWrapper.eq("type",0);
    List<Reservation> list= reservationMapper.selectList(queryWrapper);
    if (CollUtil.isEmpty(list)){
        log.info("没有超过当前时间没有完成的预约");
    }else{
         list.forEach(reservation ->{
             Long id = reservation.getId();
             reservationMapper.updateStatus(id);
         });
         log.info("修改超时预约状态成功");
    }

    }
}
